Option Strict On
Imports IICModel.BusObjects.Mappers

Namespace BusObjects

    Public Class GradeMappingUtils

        ''' <summary>
        ''' copies results mappings for a period class
        ''' </summary>
        ''' <param name="pc">Period Class object</param>
        ''' <remarks></remarks>
        Public Shared Sub copyMappingsFromPreviousPeriods(pc As PeriodClass)

            Dim pv As Period = pc.Period
            Dim diplomaId As Integer = pc.Period.DiplomaSuperTypeId.GetValueOrDefault

            Do While True
                pv = pv.getPreviousPeriod(diplomaId)
                If pv Is Nothing Then
                    Exit Do
                End If

                'copy from last period
                For Each pcv As PeriodClass In pv.Classes

                    If pcv.LessonId = pc.LessonId AndAlso pcv.GradeMappings.Count > 0 Then
                        'copy over the grademappings
                        pcv.GradeMappings.ToList.ForEach(Sub(x)
                                                             pc.AddGradeMapping(CType(x.copy, GradeMapping))
                                                         End Sub)

                        'also update classGradeMappings dictionary 
                        If classGradeMappings.ContainsKey(pc.ClassId) Then
                            classGradeMappings.Remove(pc.ClassId)
                        End If
                        Exit Sub
                    End If

                Next
            Loop

            'if we make it to here it means we are working on a new lesson that has no previoous records
            Call copyMappingsFromAnotherLesson(pc)

        End Sub


        Public Shared Sub copyMappingsFromAnotherLesson(pc As PeriodClass)

            Dim pv As Period = pc.Period
            Dim diplomaId As Integer = pc.Period.DiplomaSuperTypeId.GetValueOrDefault

                'copy from last period
            For Each otherPeriodclass As PeriodClass In pc.Period.Classes

                If otherPeriodclass.LessonId <> pc.LessonId Then
                    If otherPeriodclass.PassGrade = pc.PassGrade _
                                AndAlso otherPeriodclass.MaxGrade = pc.MaxGrade _
                                AndAlso otherPeriodclass.GradeMappings.Count > 0 Then

                        'copy over the grademappings
                        otherPeriodclass.GradeMappings.ToList.ForEach(Sub(x)
                                                                          pc.AddGradeMapping(CType(x.copy, GradeMapping))
                                                                      End Sub)

                        'also update classGradeMappings dictionary 
                        If classGradeMappings.ContainsKey(pc.ClassId) Then
                            classGradeMappings.Remove(pc.ClassId)
                        End If
                        Exit Sub
                    End If
                End If

            Next
            
        End Sub


        Private Shared classGradeMappings As Dictionary(Of Integer, List(Of GradeMapping)) = New Dictionary(Of Integer, List(Of GradeMapping))

        Public Shared Function getGradeDescription(ByVal classid As Integer?, ByVal grade As Integer?) As String

            If grade Is Nothing OrElse classid Is Nothing Then
                Return String.Empty
            Else

                If classGradeMappings Is Nothing Then
                    classGradeMappings = New Dictionary(Of Integer, List(Of GradeMapping))
                End If

                If classGradeMappings.ContainsKey(classid.GetValueOrDefault) = False Then
                    classGradeMappings.Add(classid.GetValueOrDefault, _
                                           GradeMappingDataUtils.findList("classid={0}", classid.GetValueOrDefault))
                End If

                Dim mappings As List(Of GradeMapping) = classGradeMappings.Item(classid.GetValueOrDefault)
                For Each gm As GradeMapping In mappings

                    If gm.ClassId = classid.GetValueOrDefault AndAlso _
                        grade.GetValueOrDefault >= gm.GradeFrom AndAlso _
                        grade.GetValueOrDefault <= gm.GradeTo Then

                        Return gm.GradeDescription

                    End If

                Next
                Return String.Empty
            End If

        End Function

    End Class
End Namespace
